﻿namespace DotNetUtility.Common
{
    using System;
    using System.Linq.Expressions;

    /// <summary>
    /// This class generate different expression.
    /// </summary>
    public static class ExpressionGenerator
    {
        /// <summary>
        /// It will generate the expression for addition operation.
        /// </summary>
        /// <typeparam name="T">The type will use to define the operands and return types of the addition operation. </typeparam>
        /// <returns>Expression of the addition operation</returns>
        /// Contributed by Mohammad Rahman
        public static Func<T, T, T> AdditionExpression<T>()
        {
            ParameterExpression
               parameterA = Expression.Parameter(typeof(T), "a"),
               parameterB = Expression.Parameter(typeof(T), "b");
            BinaryExpression body = Expression.Add(parameterA, parameterB);

            return Expression.Lambda<Func<T, T, T>>(body, parameterA, parameterB).Compile();
        }

        /// <summary>
        /// It will generate the expression for addition operation.
        /// </summary>
        /// <typeparam name="T">The type will use to define the operands and return types of the Multiplication operation. </typeparam>
        /// <returns>Expression of the Multiplication operation</returns>
        /// Contributed by Mohammad Rahman
        public static Func<T, T, T> MultiplicationExpression<T>()
        {
            ParameterExpression
                parameterA = Expression.Parameter(typeof(T), "a"),
                parameterB = Expression.Parameter(typeof(T), "b");
            BinaryExpression body = Expression.Multiply(parameterA, parameterB);
            return Expression.Lambda<Func<T, T, T>>(body, parameterA, parameterB).Compile();
        }
    }
}
